package com.money.tracker.generator.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.money.tracker.common.entity.rsp.IResponse;
import com.money.tracker.common.validate.QueryGroup;
import com.money.tracker.common.validate.RemoveGroup;
import com.money.tracker.generator.entity.req.GenTableReq;
import com.money.tracker.generator.entity.rsp.GenTableRsp;
import com.money.tracker.generator.service.IGenTableService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 代码生成业务表 前端控制器
 * </p>
 *
 * @author zhangshuai
 * @since 2024-10-08
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/tool/gen")
public class GenController {

    private final IGenTableService genTableService;

    /**
     * 查询代码生成列表
     */
    @SaCheckPermission("tool:gen:list")
    @PostMapping("/table/pageList")
    public IResponse<Page<GenTableRsp>> pageList(@RequestBody GenTableReq req) {
        return IResponse.success(genTableService.pageTableList(req));
    }

    /**
     * 代码生成列表查询--->修改页面
     * @param req
     * @return
     */
    @SaCheckPermission("tool:gen:query")
    @PostMapping("/table/info")
    public IResponse<GenTableRsp> getInfo(@Validated(value = QueryGroup.class) @RequestBody GenTableReq req) {
        return IResponse.success(genTableService.getInfo(req.getTableId()));
    }

    /**
     * 查询数据库列表
     */
    @SaCheckPermission("tool:gen:list")
    @PostMapping("/db/pageList")
    public IResponse<Page<GenTableRsp>> dataList(@RequestBody GenTableReq req) {
        return IResponse.success(genTableService.dbPageList(req));
    }

    /**
     * 数据导入
     *
     * @param req
     * @return {@link IResponse }<{@link ? }>
     */
    @SaCheckPermission("tool:gen:importTable")
    @PostMapping("/importTable")
    public IResponse<?> importTable(@RequestBody GenTableReq req) {
        genTableService.importTable(req.getTables());
        return IResponse.success();
    }

    /**
     * 删除代码生成
     *
     * @param req
     * @return {@link IResponse}<{@link ?}>
     */
    @SaCheckPermission("tool:gen:remove")
    @PostMapping("/remove")
    public IResponse<?> remove(@Validated(value = RemoveGroup.class) @RequestBody GenTableReq req) {
        genTableService.remove(Long.valueOf(req.getTableId()));
        return IResponse.success();
    }

    /**
     *
     * 同步数据库
     *
     * @param req
     * @return {@link IResponse}<{@link Void}>
     */
    @SaCheckPermission("tool:gen:syncDb")
    @PostMapping("/syncDb")
    public IResponse<Void> syncDb(@RequestBody GenTableReq req) {
        genTableService.syncDb(Long.parseLong(req.getTableId()));
        return IResponse.success();
    }

    /**
     *
     * 预览代码
     * @param req
     * @return {@link IResponse}<{@link ?}>
     */
    @SaCheckPermission("tool:gen:preview")
    @PostMapping("/preview")
    public IResponse<Map<String, String>> preview(@RequestBody GenTableReq req) {
        return IResponse.success(genTableService.preview(req.getTableId()));
    }

    /**
     * 修改保存代码生成业务
     * @param req
     * @return {@link IResponse}<{@link Void}>
     */
    @SaCheckPermission("tool:gen:edit")
    @PostMapping("/update")
    public IResponse<Void> update(@RequestBody GenTableReq req) {
        genTableService.update(req.getGenTable(), req.getColumns());
        return IResponse.success();
    }
}

